package com.example.understand_compose1.testcase_compose.page

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
fun ListTextView(messages: List<Message>) {
    // 已经是Column布局了
    LazyColumn(modifier = Modifier
        .padding(all = 8.dp)
        .background(
            MaterialTheme
        .colorScheme.primary),
        verticalArrangement = Arrangement.Bottom,) {
        items(messages) {
            msg -> MessageCard(msg.toString())
            // 间隔
            Spacer(modifier = Modifier.padding(10.dp))
        }
    }
}

@Preview
@Composable
fun PreviewListAndAnim() {
    ListTextView(listOf(
        Message("hello", "world"),
        Message("sucks", "org")))
}

// 状态 by remember
// remember函数为变量开辟一个存储空间
// var isExpanded by remember {mutableStateOf(false)}
// 开辟一个空间保存isExpanded变量

@Preview
@Composable
fun StateVarWithAnim() {
    // 存储状态
    var isExpanded by remember { mutableStateOf(false) }

    val surfaceColor by animateColorAsState(
        if (isExpanded) MaterialTheme.colorScheme.primary else
            MaterialTheme.colorScheme.surface
    )

    Column(
        // 点击切换状态
        modifier = Modifier.clickable { isExpanded = !isExpanded }
            .padding(all = 8.dp)
    ) {
        Text(
            text = "Friends",
            color = MaterialTheme.colorScheme.secondary
        )

        Spacer(modifier = Modifier.run { height(4.dp) })
        // 切换颜色
        Surface(
            shape = MaterialTheme.shapes.medium,
            color = surfaceColor,
            tonalElevation = 1.dp,
            modifier = Modifier.animateContentSize()
                .padding(1.dp)
        ) {
            Text(
                text = "My name is fucker fucker lcldsvlkkllcsacoegvtrnbovslnlsakn" +
                        "omcanvibpernbptrnbn" +
                        "vfdmvldfmlbmlgnoojefnvlfndscnvvfbgf",
                modifier = Modifier.padding(all = 4.dp),
                style = MaterialTheme.typography.bodyLarge,
                // 如果多行, 且isExpanded为真, 最大行为Int最大值，否则默认展示1行
                maxLines = if (isExpanded) Int.MAX_VALUE else 1
            )
        }

    }
}

